package com.demo.config;

import com.demo.entity.RetryInterceptor;
import lombok.Data;
import okhttp3.OkHttpClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Data
@Configuration
@ConfigurationProperties(prefix = "deepseek.api")
public class DeepSeekConfig {
    private String url;
    private String key;
    private Integer timeout;

    @Bean
    public OkHttpClient okHttpClient()
    {
        return new OkHttpClient.Builder()
                //重试次数为5次
                .addInterceptor(new RetryInterceptor(5))
                .connectTimeout(timeout, TimeUnit.MILLISECONDS)
                .readTimeout(timeout, TimeUnit.MILLISECONDS)
                .writeTimeout(timeout, TimeUnit.MILLISECONDS)
                .build();
    }

    @PostConstruct
    public void validateConfig(){
        if(StringUtils.isEmpty(key)){
            throw new IllegalStateException("DeepSeek API key未配置");
        }
        if(!key.startsWith("sk-")){
            throw new IllegalStateException("DeepSeek API key格式不正确");
        }
    }
}
